package org.example.classtopia.Controller;

import org.example.classtopia.Common.Result.Result;
import org.example.classtopia.Model.vo.FolloweeVO;
import org.example.classtopia.Service.UserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/follow")
public class UserFollowController {

    @Autowired
    private UserFollowService userFollowService;

    /**
     * 关注用户
     * @param followerId 关注者ID
     * @param followeeId 被关注用户ID
     * @return 操作结果
     */
    @PostMapping("/follow")
    public Result<String> follow(@RequestParam Long followerId,
                                 @RequestParam Long followeeId) {
        try {
            boolean success = userFollowService.follow(followerId, followeeId);
            if (success) {
                return Result.success("关注成功");
            } else {
                return Result.error(400, "关注失败，可能参数错误或不能关注自己");
            }
        } catch (Exception e) {
            return Result.error("服务器内部错误：" + e.getMessage());
        }
    }

    /**
     * 取消关注用户
     * @param followerId 关注者ID
     * @param followeeId 被取消关注用户ID
     * @return 操作结果
     */
    @PostMapping("/unfollow")
    public Result<String> unfollow(@RequestParam Long followerId,
                                   @RequestParam Long followeeId) {
        try {
            boolean success = userFollowService.unfollow(followerId, followeeId);
            if (success) {
                return Result.success("取消关注成功");
            } else {
                return Result.error(400, "取消关注失败");
            }
        } catch (Exception e) {
            return Result.error("服务器内部错误：" + e.getMessage());
        }
    }

    /**
     * 获取当前用户所有关注的用户列表
     * @param followerId 当前用户ID
     * @return 关注用户简要信息列表
     */
    @GetMapping("/followees")
    public Result<List<FolloweeVO>> getFollowees(@RequestParam Long followerId) {
        try {
            List<FolloweeVO> followees = userFollowService.getFollowees(followerId);
            if (followees != null) {
                return Result.success("查询成功", followees);
            } else {
                return Result.error(400, "参数错误或无数据");
            }
        } catch (Exception e) {
            return Result.error("服务器内部错误：" + e.getMessage());
        }
    }

    /**
     * 检查用户是否关注
     * @param followerId 关注者ID
     * @param followeeId 被关注者ID
     * @return 是否关注
     */
    @GetMapping("/check-follow")
    public Result<Boolean> checkFollow(@RequestParam Long followerId,
                                       @RequestParam Long followeeId) {
        try {
            boolean isFollowing = userFollowService.isFollowing(followerId, followeeId);
            return Result.success("查询成功", isFollowing);
        } catch (Exception e) {
            return Result.error("服务器内部错误：" + e.getMessage());
        }
    }
}


